package cn.house.controller.admin;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.house.common.LayuiTableResult;
import cn.house.common.Pagination;
import cn.house.common.PublicResultJosn;
import cn.house.controller.BaseController;
import cn.house.entity.HouseResource;
import cn.house.service.HouseResourceService;

/**
 * <p>
 * 房源管理 前端控制器
 * </p>
 *
 * @author yuyong
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/house-resource")
public class HouseResourceController extends BaseController {

	@Autowired
	private HouseResourceService houseResourceService;

	/**
	 * 增加公租房申请点
	 */
	public PublicResultJosn add(HouseResource houseResource) {
		houseResourceService.save(houseResource);
		return new PublicResultJosn();
	}

	/**
	 * 删除公租房申请点（禁用）
	 */
	public PublicResultJosn delete(@RequestBody List<Integer> ids) {
		List<HouseResource> entityList = ids.parallelStream().map(id -> {
			HouseResource houseResource = new HouseResource();
			houseResource.setId(id);
			houseResource.setStatus(0);
			return houseResource;
		}).collect(Collectors.toList());
		houseResourceService.updateBatchById(entityList);
		return new PublicResultJosn();
	}

	/**
	 * 根据id更新数据
	 */
	public PublicResultJosn updateById(HouseResource entity) {
		houseResourceService.updateById(entity);
		return new PublicResultJosn();
	}

	/**
	 * 根据id查询数据
	 */
	public PublicResultJosn selectById(Integer id) {
		HouseResource houseResource = houseResourceService.getById(id);
		return new PublicResultJosn(houseResource);
	}

	/**
	 * 分页模糊查询
	 */
	public LayuiTableResult selectByPage(Pagination pagination, HouseResource houseResource) {
		Wrapper<HouseResource> queryWrapper = new QueryWrapper<>(houseResource);
		IPage<HouseResource> page = new Page<>(pagination.getPage(), pagination.getPageSize());
		IPage<HouseResource> pageResult = houseResourceService.page(page, queryWrapper);
		LayuiTableResult result = new LayuiTableResult(pageResult.getTotal(), pageResult.getRecords());
		return result;
	}

}
